import {
  _decorator,
  AnimationClip,
  Color,
  Component,
  Node,
  Sprite,
  SpriteFrame,
  UITransform,
  Animation,
} from "cc";
import { Communicate } from "../../Test1/Comunicate/Communicate";
import { EventType, WarroirStates } from "../../Test1/Enums/EnumPool";
import { AnimEvents4 } from "../FSM4/AnimEvents4";
import { Kit } from "../../Test1/Kits/Kit";
import { InputKey4 } from "../KeyEvent4/InputKey4";
import { AnimState3 } from "../FSM4/AnimState4";
import { Pretreatment } from "../Pretreatment/Pretreatment";
const { ccclass, property } = _decorator;

@ccclass("Test4")
export class Test4 extends Component {
  idleAssetsFolder: string = "warrior/idle";
  attackAssetsFolder: string = "warrior/attack";
  playerNode4: Node;
  comm: Communicate;
  animEvent4: AnimEvents4;
  pretreat: Pretreatment;

  async productAnim() {
    const idleState = await this.produceIdleState();
    this.animEvent4.animMap.set(WarroirStates.WARRIOR_IDLE, idleState);

    const attackState = await this.produceAttackState();
    this.animEvent4.animMap.set(WarroirStates.WARRIOR_ATTACK, attackState);

    this.animEvent4.structuredAnimations();
  }

  async produceIdleState() {
    let framesArr = await this.pretreat.assetMap2.get(this.idleAssetsFolder);

    let idleClipDuration = 0.3;
    let idleClipName = this.idleAssetsFolder;
    let idleFrameRate = 16;
    let idleState = new AnimState3(
      idleFrameRate,
      idleClipDuration,
      undefined,
      idleClipName,
      framesArr
    );

    return idleState;
  }

  async produceAttackState() {
    let framesArr = await this.pretreat.assetMap2.get(this.attackAssetsFolder);

    let attackClipDuration = 0.4;
    let attackClipName = this.attackAssetsFolder;
    let attackClipWrapMode = AnimationClip.WrapMode.Normal;
    let attackState = new AnimState3(
      undefined,
      attackClipDuration,
      attackClipWrapMode,
      attackClipName,
      framesArr
    );
    return attackState;
  }

  ctrlByBtn(data?: string) {
    console.log("ctrlByBtn", data);
    this.animEvent4.stateTransition(data[0]);
  }

  surfaceConfigure() {
    this.pretreat.pretreatmentSetting(this.idleAssetsFolder);
  }

  bindInputKey() {
    let inputKey = this.playerNode4.getComponent(InputKey4);
    inputKey.registerMethod(this.playerNode4);
  }

  processAssetMap() {
    this.pretreat.handleAssetsMap2(this.idleAssetsFolder);
    this.pretreat.handleAssetsMap2(this.attackAssetsFolder);
    console.log("processAssetMap2", this.pretreat.assetMap2);
  }

  initializationNode() {
    let stageTest2Node = new Node("StageTest2Node");
    this.playerNode4 = new Node("playerNode4");

    stageTest2Node.addChild(this.playerNode4);
    this.node.addChild(stageTest2Node);

    this.playerNode4.addComponent(Animation);
    this.playerNode4.addComponent(UITransform);
    this.playerNode4.addComponent(Sprite);

    this.playerNode4.addComponent(Pretreatment);

    this.playerNode4.addComponent(InputKey4);
    this.pretreat = this.playerNode4.getComponent(Pretreatment);

    this.playerNode4.addComponent(AnimEvents4);
    this.animEvent4 = this.playerNode4.getComponent(AnimEvents4);
  }

  start() {
    this.processAssetMap();
    this.surfaceConfigure();
    this.productAnim();
    this.bindInputKey();
  }

  protected onLoad(): void {
    this.initializationNode();
    this.comm = Communicate.getInstance;
    this.comm.on(EventType.IDENTIFY_SPRINT, this.ctrlByBtn, this);
  }
}
